<?

/* UTF8 marker äöü */
class ccdsContentEditor
	extends ccdsBackendPlugin
{
	private $name = "ccdsContentEditor";

	function __construct()
	{
		parent::__construct();

		$this->xajax = ccdsXajax::getInstance();

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOpen", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorChangeLanguage", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentAddObj", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorSerializeSortable", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorListResults", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverview", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorGalleryUpload", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorReturnImage", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorImageUpload", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorFileUpload", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverviewUpdate", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverviewAction", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorOverviewRenameObj", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorParameter", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorParameterSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorVersionSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorMarkObjectDeleted", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorExpire", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorLinkStructure", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorCreateLinks", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorCreateParentLinks", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorDeleteLinks", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissionsClassesOpen", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissionsClassesSave", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissions", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorPermissionsSave", $this->name );



		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorEdit", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorDynamicFieldAdd", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorDynamicTemplateNew", $this->name );


		$this->xajax->register( XAJAX_CCDS_FUNCTION, "ContentEditorLinks", $this->name );

	}


	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorOpen( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		
		$objResponse->openProgress();

		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		
		$foo = new ccdsObject( $aFormValues['obj_id']);
		$obj = $foo->getObject($_SESSION['CMS']['lang'][$project['obj_key_project_id']],false);
		

		$query = "SELECT * FROM classes LEFT JOIN extensions ON (class_key_ext_id = ext_id) LEFT JOIN projects ON (ext_key_project_id=project_id) WHERE class_id='".$obj['obj_key_class_id']."' LIMIT 1";
		$res = $this->ccdSQL->query( $query );
		$row = $this->ccdSQL->fetch_assoc( $res );

		if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
		{
			$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
		}
		else
		{
			$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/page.png";
		}

		$query = "SELECT languages.* FROM languages LEFT JOIN mn_project_language ON project_language_key_language_id=lang_id  WHERE project_language_key_project_id ='".$obj['obj_key_project_id']."'";
		$res = $this->ccdSQL->query( $query );

		$objResponse->loadcommands( $this->tools->addTabManager( 'topTab', $obj['obj_id'], "<img src=\"".$icon_src."\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">".$obj['obj_name_name']."</span>" ) );

		$hide_panel = array
		(
			"list" => true,
			"overview" => false,
			"edit" => true,
			"parameter" => true
		);

		/* new overview panel */
		$objResponse->clsTabManager->addPanel( "TabManagerContent_".$obj['obj_id'], array
		(
			"id" => "Coverview".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/application.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Eigenschaften</span>",
			"content" => "<div id='ContentOverview_".$obj['obj_id']."'></div>",
			"closeable" => false
		));




		/* new content/edit panel */
		$objResponse->clsTabManager->addPanel( "TabManagerContent_".$obj['obj_id'], array
		(
			"id" => "Cedit".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/application_form_edit.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Inhalte</span>",
			"content" => "<div id='ContentEdit_".$obj['obj_id']."'></div>",
			"hide" => $hide_panel['edit'],
			"closeable" => false
		));


		/* new list panel */
		$objResponse->clsTabManager->addPanel( "TabManagerContent_".$obj['obj_id'], array
		(
			"id" => "Clist".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/application_view_list.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Liste</span>",
			"content" => "<div id='ContentList_".$obj['obj_id']."'></div>",
			"hide" => $hide_panel['list'],
			"closeable" => false
		));

		/* new parameter/settings panel */
		$objResponse->clsTabManager->addPanel( "TabManagerContent_".$obj['obj_id'], array
		(
			"id" => "Cparameter".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/application_edit.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Voreinstellungen</span>",
			"content" => "<div id='ContentParameter_".$obj['obj_id']."'></div>",
			"hide" => $hide_panel['parameter'],
			"closeable" => false
		));

		/* new permission panel */
		$objResponse->clsTabManager->addPanel( "TabManagerContent_".$obj['obj_id'], array
		(
			"id" => "Cpermissions".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/group_edit.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Benutzerrechte</span>",
			"content" => "<div id='ContentPermissions_".$obj['obj_id']."'></div>",
			"hide" => true,
			"closeable" => false
		));

		/* new classes panel */
		$objResponse->clsTabManager->addPanel( "TabManagerContent_".$obj['obj_id'], array
		(
			"id" => "CpermissionClasses".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/package_add.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Klassen</span>",
			"content" => "<div id='ContentPermissionClasses_".$obj['obj_id']."'></div>",
			"hide" => true,
			"closeable" => false
		));

		$objResponse->clsTabManager->addPanel( "TabManagerContent_".$obj['obj_id'], array
		(
			"id" => "Clinks".$obj['obj_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/package_add.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Links</span>",
			"content" => "<div id='ContentLinks_".$obj['obj_id']."'></div>",
			"hide" => true,
			"closeable" => false
		));

		$objResponse->flush();
		
		$objResponse->loadcommands( $this->ContentEditorOverview( $aFormValues ) );
		$objResponse->updateProgress(10);
		$objResponse->flush();
		$objResponse->loadcommands( $this->ContentEditorEdit( $aFormValues ) );
		$objResponse->updateProgress(30);
		$objResponse->flush();
		$objResponse->loadcommands( $this->ContentEditorList( $aFormValues ) );
		$objResponse->updateProgress(50);
		$objResponse->flush();
		$objResponse->loadcommands( $this->ContentEditorParameter( $aFormValues ) );
		$objResponse->updateProgress(60);
		$objResponse->flush();
		$objResponse->loadcommands( $this->ContentEditorPermissions( $aFormValues ) );
		$objResponse->loadcommands( $this->ContentEditorPermissionsClassesOpen( $aFormValues ) );
		$objResponse->loadcommands( $this->ContentEditorLinks( $aFormValues ) );

		$objResponse->updateProgress(80);
		$objResponse->flush();

		$objResponse->clsTabManager->showPanel( "TabManagerContent_".$obj['obj_id'], "Cedit".$obj['obj_id'] );


		$objResponse->clsTabManager->on("show", "TabManagerContent_".$obj['obj_id'], "Coverview".$obj['obj_id'],
		"
function() {
	try {
				xajax.request({ccdsfun:'ContentEditorOverview',ccdsclass:'ccdsContentEditor'},
				{
					parameters:[{
						obj_id:'".$obj['obj_id']."',
						project_id:'".$obj['obj_key_project_id']."'
					}]
				}
				);
	} catch(ex) {
	
	}		
}
		");
		$objResponse->updateProgress(100);
		$objResponse->flush();

		$objResponse->closeProgress();

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorList( $aFormValues )
	{

		$_SESSION['CMS']['ContentEditor'] = "list";

		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}
		

		if ( $aFormValues['parameter'] == "undefined" )
			$aFormValues['parameter'] = array ();
			
		$foo = new ccdsObject($aFormValues['obj_id']);
		$obj= $foo->getObject($_SESSION['CMS']['lang'][$aFormValues['project_id']],false);

		$this->smarty->assign( "obj", $obj );

		$query = "SELECT * FROM classes LEFT JOIN extensions ON (class_key_ext_id = ext_id) WHERE ext_key_project_id='".$aFormValues['project_id']."' ORDER BY ext_name,class_name";
		$res = $this->ccdSQL->query($query);
		$classes = array();
		while ($row = $this->ccdSQL->fetch_assoc($res)) $classes[] = $row;
		
		$this->smarty->assign( "classes", $classes );

		$template = $this->config->get( 'TPL_DIR' )."content_list.tpl";

		$this->smarty->assign( "parameter", $aFormValues['parameter'] );
		$sHtml = $this->smarty->fetch( $template );

		$objResponse->assign( "ContentList_".$obj['obj_id'], "innerHTML", $sHtml );

		if ( $obj['obj_key_tpl_backend_id'] == 0 )
			$objResponse->loadcommands( $this->ContentEditorListResults( array(
				"obj_id" => $aFormValues['obj_id'],
				"listsearch_range" => $aFormValues['obj_id'],
				"project_id" => $aFormValues['project_id']
			) ) );
		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorListResults( $aFormValues )
	{
		$objResponse = new ccdsResponse();

		$foo = new ccdsObject($aFormValues['obj_id']);
		$obj= $foo->getObject($_SESSION['CMS']['lang'][$aFormValues['project_id']],false);

		$this->smarty->assign( "obj", $obj );

		if ( !is_array($aFormValues['parameter']) )
			$aFormValues['parameter'] = array ();

		if ( !isset( $aFormValues['parameter']['offset'] ) )
			$aFormValues['parameter']['offset'] = 0;
		$list_results_per_page = 25;

		if ( !is_array( $aFormValues['parameter'] ) )
			$aFormValues['parameter'] = array ();

		if ( $aFormValues['parameter']['order'] == "" )
			$aFormValues['parameter']['order'] = "position";

		if ( $aFormValues['parameter']['ordertype'] == "" )
			$aFormValues['parameter']['ordertype'] = "asc";
		$this->smarty->assign( "parameter", $aFormValues['parameter'] );
		
		
		$sql_parent = !is_numeric($aFormValues['listsearch_range']) ? '' : ' AND obj_parent_obj_id = '.$aFormValues['listsearch_range'].' ';
		$sql_class = !is_numeric($aFormValues['listsearch_class']) ? '' : ' AND obj_key_class_id = '.$aFormValues['listsearch_class'].' ';
		
		$query = "
           SELECT
           count(obj_id) as anzahl
           FROM
           obj_index
           LEFT OUTER JOIN
           obj_names
           ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='".$_SESSION['CMS']['lang'][$obj['obj_key_project_id']]."')
           WHERE 1            
           ".$sql_parent.$sql_class;

		if ( $aFormValues['parameter']['sq'] != "" )
		{
			$query .= " AND obj_name_name LIKE '".$aFormValues['parameter']['sq']."%' ";
		}
		$res = $this->ccdSQL->query( $query );
		$row = $this->ccdSQL->fetch_assoc( $res );
		$element_count = $row['anzahl'];

		$pager = array ();
		$anzahl = $row['anzahl'];

		$pages = ceil( $anzahl / $list_results_per_page );
		$pagertmp = array ();

		for ( $i = 0; $i < $pages; $i++ )
		{
			$pagertmp[] = ($i);
		}
		$pager['PAGER'] = $pagertmp;
		$pager['PAGES'] = $pages;

		$this->smarty->assign( "pager", $pager );

		$query = "
           SELECT
           *
           FROM
           obj_index
           LEFT OUTER JOIN
           obj_names
           ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='".$_SESSION['CMS']['lang'][$obj['obj_key_project_id']]."')
           LEFT JOIN classes ON (obj_key_class_id = class_id)
           
           WHERE 1
           ".$sql_parent.$sql_class;

		if ( $aFormValues['parameter']['sq'] != "" )
		{
			$query .= " AND obj_name_name LIKE '%".$aFormValues['parameter']['sq']."%' ";
		}

		switch ( $aFormValues['parameter']['order'] )
		{
			case "name":

				$query .= "
                 ORDER BY obj_name_name ".$aFormValues['parameter']['ordertype']."
                 ";
				break;

			case "created":

				$query .= "
                 ORDER BY obj_created ".$aFormValues['parameter']['ordertype'].", obj_name_name asc
                 ";
				break;

			case "last_edit":

				$query .= "
                 ORDER BY obj_last_edit ".$aFormValues['parameter']['ordertype'].", obj_name_name asc
                 ";
				break;

			case "position":

				$query .= "
                 ORDER BY obj_position ".$aFormValues['parameter']['ordertype'].", obj_name_name asc
                 ";
				break;

			case "class_name":

				$query .= "
                 ORDER BY class_name ".$aFormValues['parameter']['ordertype'].", obj_name_name asc
                 ";
				break;

				default:
				$query .="
                 ORDER BY obj_name_name
                 ";
				break;
		}

		$query .= "
           LIMIT ".$list_results_per_page."
           OFFSET ".( $aFormValues['parameter']['offset'] * $list_results_per_page )."
           ";
		$res = $this->ccdSQL->query( $query );
		$elements = array ();
		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$obj_name = $this->tools->getObjectName( $row['obj_id'], $_SESSION['CMS']['lang'][$obj['obj_key_project_id']], true );
			$row = array_merge( $row, $obj_name );
			$elements[] = $row;
		}
		$this->smarty->assign( "elements", $elements );
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_list_results.tpl" );
		$objResponse->assign( "DivContentEditorListResults_".$obj['obj_id'], "innerHTML", $sHtml );

		foreach ( $elements as $elm )
		{
			$objResponse->script( "CMS.makeEditable('sortable".$elm['obj_id']."');" );
		}
		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorOverview( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}
		
		$this->smarty->assign("project_id",$aFormValues['project_id']);

		$_SESSION['CMS']['ContentEditor'] = "overview";
		$obj = $this->tools->getObjectDetails( $aFormValues['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
		;
		$obj['permissions'] = $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['lang'][$aFormValues['project_id']], $obj['obj_id'], $_SESSION['CMS']['user_level'] );
		$tpl_file_org = "content_overview.tpl";
 
		$obj['days'] = array ();

		for ( $i = 1; $i <= 31; $i++ )
			$obj['days'][] = $i;
		$obj['months'] = array ();

		for ( $i = 1; $i <= 12; $i++ )
			$obj['months'][] = $i;
		$this->smarty->assign( "obj", $obj );

		if ( $obj['obj_link_obj_id'] > 0 )
		{
			$query = "SELECT obj_key_class_id FROM obj_index WHERE obj_id='".$obj['obj_link_obj_id']."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$row = $this->ccdSQL->fetch_assoc( $res );
		}
		$project_languages = array ();
		$query = "
           SELECT * FROM mn_project_language LEFT JOIN languages ON project_language_key_language_id=lang_id WHERE project_language_key_project_id='".$obj['obj_key_project_id']."'";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$project_languages[] = $row;
		}
		$this->smarty->assign( "project_languages", $project_languages );

		$obj_public = array ();
		$query = "
           SELECT * FROM obj_index_public WHERE objp_key_obj_id='".$obj['obj_id']."'";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$obj_public[$row['objp_key_lang_id']] = 1;
		}
		$this->smarty->assign( "obj_public", $obj_public );

		$obj_key_class_id = $obj['obj_key_class_id'];

		$query = "SELECT
							*
							FROM
							classes
							LEFT JOIN
							permissions_class_2_class ON class_2_class_child_id = class_id
							
							LEFT OUTER JOIN permissions_obj_index_2_class ON
							(
								obj_index_class_key_class_id = class_2_class_child_id
								AND
								obj_index_class_key_obj_id = '".$obj['obj_id']."'
							)
							
							WHERE
							class_2_class_node_id='".$obj['obj_key_class_id']."'
							AND (
								class_2_class_visible=1
								OR 
								obj_index_class_key_class_id IS NOT NULL
							)
							ORDER BY
							class_name
           ";

		$res = $this->ccdSQL->query( $query );
		$classes = array ();

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$classes[] = $row;
		}
		$this->ccdSQL->free_result( $res );
		$this->smarty->assign( "classes", $classes );

		$templates = array ();
		$query = "SELECT templates.* FROM templates 
							LEFT JOIN mn_template_class ON (tpl_class_key_tpl_id = tpl_id)							
							WHERE 
								tpl_class_key_class_id='".$obj['obj_key_class_id']."' 
							ORDER BY tpl_name";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			$templates[] = $row;
		$this->ccdSQL->free_result( $res );
		$this->smarty->assign( "templates", $templates );

		$linked_objects = array ();

		$query = "SELECT * FROM obj_index WHERE obj_link_obj_id='".$obj['obj_id']."' order by obj_position";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$row = $this->tools->getObjectDetails( $row['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
			$row['PATH'] = $this->tools->getRecursivePath( $row['obj_id'], false, true );
			$linked_objects[] = $row;
		}
		$linked_count = $this->ccdSQL->num_rows( $res );
		$this->ccdSQL->free_result( $res );
		$this->smarty->assign( "linked_objects", $linked_objects );
		$this->smarty->assign( "linked_count", $linked_count );

		if ( $obj['obj_parent_obj_id'] > 0 )
		{
			$query = "SELECT COUNT(obj_id) as anzahl FROM obj_index WHERE obj_link_obj_id = '".$obj['obj_parent_obj_id']."'";
			$res = $this->ccdSQL->query( $query );
			$row = $this->ccdSQL->fetch_assoc( $res );
			$this->ccdSQL->free_result( $res );
			$parent_link_count = $row['anzahl'];
			$this->smarty->assign( "parent_link_count", $parent_link_count );

			$query = "SELECT * FROM obj_index WHERE obj_link_obj_id = '".$obj['obj_parent_obj_id']."'";
			$res = $this->ccdSQL->query( $query );
			$parent_links = array ();

			while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			{
				$row = $this->tools->getObjectDetails( $row['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
				$row['PATH'] = $this->tools->getRecursivePath( $row['obj_id'], false, true );

				if ( $x = $this->ccdSQL->fetch_assoc( $this->ccdSQL->query( "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id='".$row['obj_id']."' AND obj_link_obj_id='".$aFormValues['obj_id']."'" ) ) )
				{
					$row['LINKED'] = 1;
				}
				$parent_links[] = $row;
				usort( $parent_links, "sort_path" );
			}
			$this->ccdSQL->free_result( $res );
			$this->smarty->assign( "parent_links", $parent_links );
		}else
		{
			$parent_links = array ();
			$parent_link_count = 0;
			$this->smarty->assign( "parent_link_count", $parent_link_count );
			$this->smarty->assign( "parent_links", $parent_links );
		}

		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' ).$tpl_file_org );
		$objResponse->assign( "ContentOverview_".$obj['obj_id'], "innerHTML", $sHtml );
		//$objResponse->loadcommands($this->ContentEditorLinkStructure(array("obj_id" => 0,"current_obj_id"=>$aFormValues['obj_id'])));

		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorLinks( $aFormValues )
	{
		$objResponse = new ccdsResponse();

		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_links.tpl" );
		$objResponse->assign( "ContentLinks_".$aFormValues['obj_id'], "innerHTML", $sHtml );

		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorLinkStructure( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$query = "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id='".$aFormValues['obj_id']."' AND obj_deleted=0 ORDER BY obj_position";
		$res = $this->ccdSQL->query( $query );
		$structure = array ();

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{

			$structure[] = $this->tools->getObjectDetails( $row['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
		}
		$this->ccdSQL->free_result( $res );
		$this->smarty->assign( "current_obj_id", $aFormValues['current_obj_id'] );
		$this->smarty->assign( "structure", $structure );
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_structure_listing.tpl" );
		$objResponse->assign( "linkstructure_obj".$aFormValues['obj_id'], "innerHTML", $sHtml );

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorDeleteLinks( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( is_array( $aFormValues['dellink'] ) )
			foreach ( $aFormValues['dellink'] as $link )
			{
				$this->ccdSQL->query( "DELETE FROM obj_index WHERE obj_id='".$link."'" );
			}
		$objResponse->loadcommands( $this->ContentEditorOverview( array ("obj_id" => $aFormValues['obj_id'])) );
		return $objResponse;
	}


	// -------------------------------------------------------------------------------------------------------------------
	function ContentEditorCreateParentLinks( $aFormValues )
	{
		$objResponse = new ccdsResponse();
		$obj = $this->tools->getObjectDetails( $aFormValues['obj_id'], $_SESSION['CMS']['user_lang'], false );

		if ( $aFormValues['link_parents'] == 1 )
		{
			$query = "SELECT * FROM obj_index WHERE obj_link_obj_id='".$obj['obj_parent_obj_id']."'";
			$res = $this->ccdSQL->query( $query );

			while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			{
				CreateLinkObject( $aFormValues['obj_id'], $row['obj_id'], false );
			}
		}
		$objResponse->loadcommands( $this->ContentEditorOverview( array ("obj_id" => $aFormValues['obj_id'])) );
		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorCreateLinks( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( ( $aFormValues['link_current_obj'] == 1 ) OR ( $aFormValues['link_below'] == 1 ) )
		{
			//	if ($aFormValues['link_below'] == 1) {
			if ( is_array( $aFormValues['link_into'] ) )
			{
				foreach ( $aFormValues['link_into'] as $dest_obj_id )
				{
					if ( $aFormValues['link_current_obj'] == 1 )
					{
						$aFormValues['link_below'] == 1 ? $recursive = true : $recursive = false;
						CreateLinkObject( $aFormValues['obj_id'], $dest_obj_id, $recursive );
					}else
					{
						$query = "SELECT * FROM obj_index WHERE obj_parent_obj_id='".$aFormValues['obj_id']."' AND obj_deleted=0 AND obj_link_obj_id=0";
						$res = $this->ccdSQL->query( $query );

						while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
						{
							CreateLinkObject( $row['obj_id'], $dest_obj_id, true );
						}
					}
				}
			}
		//	}
		}
		$objResponse->loadcommands( $this->ContentEditorOverview( array ("obj_id" => $aFormValues['obj_id'])) );
		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

		function ContentEditorSerializeSortable($aFormValues) {
		
		$objResponse = new ccdsResponse();
		$obj_id = str_replace("sortable","",$aFormValues['obj_id']);
	
	
		$query = "SELECT * FROM obj_index WHERE obj_id='".$obj_id."'";
		$res = $this->ccdSQL->query($query);
		$obj = $this->ccdSQL->fetch_assoc($res);
	
		if ($obj['obj_position'] < $aFormValues['position'])	{
			$this->ccdSQL->query("UPDATE obj_index SET obj_position=obj_position-1 WHERE obj_position >".$obj['obj_position']." AND obj_position <= ".$aFormValues['position']." AND obj_parent_obj_id='".$obj['obj_parent_obj_id']."'");
			$this->ccdSQL->query("UPDATE obj_index SET obj_position='".$aFormValues['position']."' WHERE obj_id='".$obj_id."'");
		} elseif ($obj['obj_position'] > $aFormValues['position']) {
			$this->ccdSQL->query("UPDATE obj_index SET obj_position=obj_position+1 WHERE obj_position >= ".$aFormValues['position']." AND obj_position < ".$obj['obj_position']." AND obj_parent_obj_id='".$obj['obj_parent_obj_id']."'");
			$this->ccdSQL->query("UPDATE obj_index SET obj_position='".$aFormValues['position']."' WHERE obj_id='".$obj_id."'");
		}
		
		$query = "SELECT obj_id FROM obj_index WHERE obj_parent_obj_id = '".addslashes($obj['obj_parent_obj_id'])."' ORDER BY obj_position";
		$res = $this->ccdSQL->query($query);
		while ($row = $this->ccdSQL->fetch_assoc($res))
		{
			$new_positions[]=$row['obj_id'];
		}
	
		if (0 < $obj_id)
		{
			$query = "SELECT obj_parent_obj_id FROM obj_index WHERE obj_link_obj_id = '".$obj_id."' ";
			$res = $this->ccdSQL->query($query);
			while ( $row = $this->ccdSQL->fetch_assoc($res) )
			{
				$foo = sortLinkedObjects( $row['obj_parent_obj_id'], $new_positions );
			}
		}
		
		
		$objResponse->script("editComplete('".$aFormValues['position']."','".$aFormValues['obj_id']."')");
		$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
				(
					"project_id" => $obj['obj_key_project_id'],
					"obj_parent_obj_id" => $obj['obj_parent_obj_id']
				),true);
	
		$objResponse->loadCommands($this->ContentEditorList(array("obj_id"=>$obj['obj_parent_obj_id'])));
	
		
		return $objResponse;
	}
	
	// -------------------------------------------------------------------------------------------------------------------
	
	function sort_array(&$orgarray,  &$sortarray) {
		$new = array();
		$tmp = array();
		$not_found = array();	
		foreach ($sortarray as $key => $value) {
			$pos = array_search((string) $value,$orgarray);
			if (false === $pos) {
				$not_found[$key] = $value;
			} else {
				$tmp[$pos]=$value;		
			}
		}
		$diff = array_diff($sortarray, $tmp);
		ksort($tmp);
		
		
		foreach ($diff as $key => $value) 
		{
			array_insert($tmp,$key,$value);
			
		}
		return $tmp;
	}
	
	
	// -------------------------------------------------------------------------------------------------------------------
	function sortLinkedObjects($obj_parent_obj_id, $new_positions) {
		$query = "SELECT * FROM obj_index WHERE obj_parent_obj_id='".$obj_parent_obj_id."'  ORDER BY obj_position";
		$res = $this->ccdSQL->query($query);
		$i=1;
		$current_positions = array();
		$ids = array();
		while ($row = $this->ccdSQL->fetch_assoc($res)) 
		{
			$pos = $row['obj_link_obj_id'] > 0 ? $row['obj_link_obj_id'] : $row['obj_id'];
			$current_positions[] = $pos;
			$ids[$pos] = $row['obj_id'];
			
		}
		sort_array($new_positions,$current_positions);
		$i=1;
		foreach ($new_positions as $obj_id) {
			$query = "UPDATE obj_index SET obj_position = '".$i."' WHERE obj_id = '".$ids[$obj_id]."' AND obj_parent_obj_id='".$obj_parent_obj_id."' LIMIT 1";
			$this->ccdSQL->query($query);
			$i++;
		}
	
	}	

	// -------------------------------------------------------------------------------------------------------------------
	function ContentAddObj( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( $aFormValues['obj_key_class_id'] == "" ) {
			$objResponse->alert("Keine Klasse gewählt!");
			return $objResponse;
		}
		else
		{
			if ( "" == $aFormValues['project_id'] )
			{
				$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_parent_obj_id'] )."' LIMIT 1";
				$res = $this->ccdSQL->query( $query );
				$project = $this->ccdSQL->fetch_assoc( $res );
				$aFormValues['project_id'] = $project['obj_key_project_id'];
			}

			$obj = new ccdsObject(false,array(
				"obj_parent_obj_id" => $aFormValues['obj_parent_obj_id'],
				"class_id" => $aFormValues['obj_key_class_id'],
				"obj_name" => $aFormValues['obj_name'],
				"lang_id" => $_SESSION['CMS']['lang'][$aFormValues['project_id']],
				"user_id" => $_SESSION['CMS']['user_id'],
				"obj_is_navi" => false,
				"project_id" => $aFormValues['project_id']
				
			));

			if ( 0 < $aFormValues['obj_parent_obj_id'] ) {
				$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
					(
						"project_id" => $aFormValues['project_id'],
						"obj_parent_obj_id" => $aFormValues['obj_parent_obj_id']
					),true);			
			} else {
				$objResponse->loadClassCommand( "ccdsDefaultPlugin", "initNavi");			
			}
			
			if (1 == $aFormValues['first_pos']) $objResponse->loadcommands(
				$this->ContentEditorSerializeSortable(array(
					'obj_id' => $obj->getObjId(),
					'position' => 1
				))
			);
			
			if (1 == $aFormValues['jump_to_obj'])
				$objResponse->loadcommands( $this->ContentEditorOpen( array ("obj_id" => $obj->getObjId())) );

			$_SESSION['CMS']['last_inserted_class'] = $aFormValues['obj_key_class_id'];
			$aFormValues['jump_to_obj'] = 1 == $aFormValues['jump_to_obj'] ? true : false;
			$_SESSION['CMS']['jump_to_obj'] = $aFormValues['jump_to_obj'];
			
		}
		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorOverviewRenameObj( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id,obj_parent_obj_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$obj = $this->tools->getObjectDetails( $aFormValues['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );

		$query = "SELECT * FROM obj_names WHERE obj_name_key_obj_id='".$aFormValues['obj_id']."' AND obj_name_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."'";
		$res = $this->ccdSQL->query( $query );

		if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$this->ccdSQL->query( "UPDATE
                   obj_names
                   SET obj_name_name='".addslashes( $aFormValues['obj_name_name'] )."',
                   obj_name_serialized ='".$this->tools->serialize_name( $aFormValues['obj_name_name'], $obj['obj_parent_obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], $aFormValues['obj_id'] )."'
                   WHERE obj_name_id='".$row['obj_name_id']."'" );
		}
		else
		{
			$query = " INSERT INTO obj_names
               (
               obj_name_name,
               obj_name_key_obj_id,
               obj_name_key_lang_id,
               obj_name_serialized
               )
               VALUES (
               '".addslashes( $aFormValues['obj_name_name'] )."',
               '".$aFormValues['obj_id']."',
               '".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."',
               '".$this->tools->serialize_name( $aFormValues['obj_name_name'], $obj['obj_parent_obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], $aFormValues['obj_id'] )."'
               )
               ";
			$this->ccdSQL->query( $query );
		}

		if ( $aFormValues['obj_is_cacheable'] == "" )
		{
			$aFormValues['obj_is_cacheable'] = 0;
		}

		if ( $aFormValues['obj_is_navi'] == "" )
		{
			$aFormValues['obj_is_navi'] = 0;
		}

		$this->ccdSQL->query( "UPDATE obj_index SET
               obj_cacheable='".$aFormValues['obj_cacheable']."',
               obj_is_navi='".$aFormValues['obj_is_navi']."', obj_last_edit='".time()."',
               obj_last_edit_key_user_id='".$_SESSION['CMS']['user_id']."',
               obj_key_tpl_id='".$aFormValues['obj_key_tpl_id']."' WHERE
               obj_id='".$aFormValues['obj_id']."' " );

		if ( $aFormValues['apply_linked'] == 1 )
		{
			$query = "SELECT obj_id FROM obj_index WHERE obj_link_obj_id='".$aFormValues['obj_id']."'";
			$res = $this->ccdSQL->query( $query );

			while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			{
				$this->ccdSQL->query( "UPDATE obj_index SET obj_cacheable='".$aFormValues['obj_cacheable']."',	obj_is_navi='".$aFormValues['obj_is_navi']."', obj_last_edit='".time()."',
                       obj_last_edit_key_user_id='".$_SESSION['CMS']['user_id']."',
                       obj_key_tpl_id='".$aFormValues['obj_key_tpl_id']."' WHERE
                       obj_id='".$row['obj_id']."' " );
			}
		} 


		$query = "SELECT * FROM classes LEFT JOIN extensions ON (class_key_ext_id = ext_id) LEFT JOIN projects ON (ext_key_project_id=project_id) WHERE class_id='".$obj['obj_key_class_id']."' LIMIT 1";
		$res = $this->ccdSQL->query( $query );
		$row = $this->ccdSQL->fetch_assoc( $res );

		if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
		{
			$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
		}
		else
		{
			$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/page.png";
		}

		$objResponse->clsTabManager->setTitle( 'topTab','tab_'.$aFormValues['obj_id'],"<img src=\"".$icon_src."\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">".$aFormValues['obj_name_name']."</span>" );
		$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array ("project_id" => $aFormValues['project_id'],"obj_parent_obj_id" => $aFormValues['obj_id']));

		return $objResponse;
	}


	// -------------------------------------------------------------------------------------------------------------------

	private function ContentEditorEditEvents( &$objResponse, &$FormField, &$obj )
	{
		switch ( $FormField['formtype'] )
		{

			case 'html':
				$objResponse->script( "
            tinyMCE.execCommand('mceAddControl', false, 'obj_".$obj['obj_id']."_".$FormField['fieldname']."');
            " );
				$objResponse->clsTabManager->on( 'destroy', "TabManagerContent_".$obj['obj_id'], "Cedit".$obj['obj_id'], "function() {
	            	tinyMCE.execCommand('mceRemoveControl', false, 'obj_".$obj['obj_id']."_".$FormField['fieldname']."');
								}" );

				break;

			case 'file':

				if ($FormField['default_path'] != "") {
					$objResponse->clsSwfUpload->transField( "up_".$obj['obj_id']."_".$FormField['fieldname'], array
					(
						"file_types_description" => "Bilder",
						"file_size_limit" => "3 MB",
						"file_types" => "*.jpg;*.gif;*.png;*.tiff;*.jpeg",
						"file_queue_limit" => 1,
						"post_params" => array
						(
							"PHPSESSID" => session_id(),
							"path" => $FormField['default_path'],
							"root" => "media"
						)
					), false );
	
					$objResponse->clsTabManager->on( 'destroy', "TabManagerContent_".$obj['obj_id'], "Cedit".$obj['obj_id'], "function() {
											xajax.ext.SWFupload.tools.destroyField('".$FormField['fieldname']."_".$obj['obj_id']."_up"."');
								}" );
				}
				break;

			case 'image':
				if ( isset($FormField['default_path']) )
				{
					$objResponse->clsSwfUpload->transField( "up_".$obj['obj_id']."_".$FormField['fieldname'], array
					(
						"file_types_description" => "Bilder",
						"file_size_limit" => "3 MB",
						"file_types" => "*.jpg;*.gif;*.png;*.tiff;*.jpeg",
						"file_queue_limit" => 1,
						"post_params" => array
						(
							"PHPSESSID" => session_id(),
							"path" => $FormField['default_path'],
							"root" => "media"
						)
					), false );
	
					$objResponse->clsTabManager->on( 'destroy', "TabManagerContent_".$obj['obj_id'], "Cedit".$obj['obj_id'], "function() {
											xajax.ext.SWFupload.tools.destroyField('".$FormField['fieldname']."_".$obj['obj_id']."_up"."');
								}" );
				}
				break;


			case 'gallery':
				if ( isset($FormField['default_path']) )
				{
					$objResponse->clsSwfUpload->transField( $FormField['fieldname']."_".$obj['obj_id']."_multiup", array
					(
						"file_types_description" => "Bilder",
						"file_size_limit" => "3 MB",
						"file_types" => "*.jpg;*.gif;*.png;*.tiff;*.jpeg",
						"post_params" => array
						(
							"PHPSESSID" => session_id(),
							"path" => $FormField['default_path'],
							"root" => "media"
						)
					), true );
	
					$objResponse->clsTabManager->on( 'destroy', "TabManagerContent_".$obj['obj_id'], "Cedit".$obj['obj_id'], "function() {
											xajax.ext.SWFupload.tools.destroyField('".$FormField['fieldname']."_".$obj['obj_id']."_multiup"."');
										}" );
	
					$objResponse->script( "
						$('#GallerySortable_".$obj['obj_vers_id'].'_'.$FormField['fieldname']."').sortable({});
					" );
				}
				break;

			default:

			break;
		}
	}


	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorEdit( $aFormValues )
	{
		$objResponse = new ccdsResponse();

		$_SESSION['CMS']['ContentEditor'] = "edit";

		if ( "" == $aFormValues['project_id'] )
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$this->smarty->assign('project_id',$aFormValues['project_id']);

		$query =
			"SELECT * FROM obj_index LEFT OUTER JOIN obj_names ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."')LEFT OUTER JOIN obj_versions ON obj_vers_key_obj_id = obj_id WHERE obj_id='"
			.$aFormValues['obj_id']."'";
		$res = $this->ccdSQL->query( $query );
		$obj = $this->ccdSQL->fetch_assoc( $res );
		$obj['permissions'] = $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'], $_SESSION['CMS']['user_level'] );
		$this->smarty->assign( "obj", $obj );

		if ( $aFormValues['obj_vers_id'] == "" )
		{
			$query = "SELECT * FROM obj_versions WHERE obj_vers_key_obj_id='".$aFormValues['obj_id']."' AND obj_vers_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."' ORDER BY obj_vers_created desc LIMIT 1";
		}
		else
		{
			$query = "SELECT * FROM obj_versions WHERE obj_vers_id='".$aFormValues['obj_vers_id']."' AND obj_vers_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."' ORDER BY obj_vers_created desc LIMIT 1";
		}

		$res = $this->ccdSQL->query( $query );

		if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$obj_vers_id = $row['obj_vers_id'];
		}
		else
		{
			$obj_vers_id = false;
		}


		$this->smarty->assign( "obj_vers_id", $obj_vers_id );

		$foo = new ccdsObject( $obj['obj_id']);
		if ($foo->hasContent())
			$aFormFields = $foo->getEditArray( $obj_vers_id, $_SESSION['CMS']['lang'][$aFormValues['project_id']] );

		$this->smarty->assign( "aFormFields", $aFormFields );

		$obj_versions = array ();
		$query = "SELECT * FROM obj_versions LEFT OUTER JOIN user ON obj_vers_creator = user_id
           WHERE obj_vers_key_obj_id='".$aFormValues['obj_id']."' AND obj_vers_key_lang_id = '".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."' ORDER BY obj_vers_created desc LIMIT 10";
		$res = $this->ccdSQL->query( $query );
		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$obj_versions[] = $row;
		}

		$this->smarty->assign( "obj_versions", $obj_versions );

		$query = "SELECT languages.* FROM languages LEFT JOIN mn_project_language ON project_language_key_language_id=lang_id  WHERE project_language_key_project_id ='".$obj['obj_key_project_id']."'";
		$res = $this->ccdSQL->query( $query );

		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_edit.tpl" );
		$objResponse->assign( "ContentEdit_".$aFormValues['obj_id'], "innerHTML", $sHtml );
		$destroy_callback = "";

		$foo = "";

		if ( is_array( $aFormFields ) )
		{
			foreach ( $aFormFields as $FormField )
			{
				$this->ContentEditorEditEvents( $objResponse, $FormField, $obj );

				if ( "fieldset" == $FormField['formtype'] )
				{
					if ( !is_array($FormField['fields']) ) $FormField['fields'] = array();
					foreach ( $FormField['fields'] as $Field )
					{
						$this->ContentEditorEditEvents( $objResponse, $Field, $obj );
					} 
				}
			}
		}
		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorGalleryUpload( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		if ( is_array( $_FILES[$aFormValues['filefield']] ) )
		{
			switch ( $aFormValues['root'] )
			{
				case "media":
					$root_dir = $this->config->get( 'MEDIA_DIR' );
	
					$query = "SELECT * FROM projects WHERE project_id='".addslashes($aFormValues['project_id'])."' LIMIT 1";
					$res = $this->ccdSQL->query($query);
					if ($row = $this->ccdSQL->fetch_assoc($res)) 
					{
						$aFormValues['dir'] = str_replace("/".$row['project_dir'],'',$aFormValues['dir']);
						$aFormValues['dir'] = "/".$row['project_dir'].$aFormValues['dir'];
					}
					break;

				case "extensions":
					$root_dir = $this->config->get( 'EXTENSION_DIR' );
					break;

					default : $root_dir = $this->config->get('MEDIA_DIR');
					break;
			}

			$dest = $root_dir.$aFormValues['path']."/".$_FILES[$aFormValues['filefield']]['name'];

			$res = move_uploaded_file( $_FILES[$aFormValues['filefield']]['tmp_name'], $dest );
			if (is_file($dest)) chmod($dest,$this->config->get('FILE_CHMOD'));

			if ( $res )
			{
				$query =
					"SELECT * FROM obj_index LEFT OUTER JOIN obj_names ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]
					."')LEFT OUTER JOIN obj_versions ON obj_vers_key_obj_id = obj_id WHERE obj_id='"
					.$aFormValues['obj_id']."'";
				$res = $this->ccdSQL->query( $query );
				$obj = $this->ccdSQL->fetch_assoc( $res );
				$this->smarty->assign( "obj", $obj );

				$img = array
				(
					"picture_filename" => $_FILES[$aFormValues['filefield']]['name'],
					"picture_dir" => $aFormValues['path']."/",
					"picture_description" => ""
				);

				$aFormValues['count']++;

				$id = rand( $aFormValues['count'], 100000 );

				$this->smarty->assign( "count", $id );
				$this->smarty->assign( "img", $img );
				$this->smarty->assign( "fieldname", $aFormValues['fieldname'] );

				$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."forms/gallery_append.tpl" );
				$objResponse->contextAppend( "innerHTML", $sHtml );

				$objResponse->script( '$("#gallery_'.$aFormValues['fieldname'].'_'.$id.'_appear").slideDown("slow");' );
				$objResponse->call( 'Sortable.create', 'GallerySortable_'.$obj['obj_vers_id'].'_'.$aFormValues['fieldname'], array
				(
					"tag" => "div",
					"overlap" => 'horizontal',
					"constraint" => false
				));
			}
		}

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorFileUpload( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

	if ( is_array( $_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']] ) )
		{
			switch ( $aFormValues['root'] )
			{
				case "media":
					$root_dir = $this->config->get( 'MEDIA_DIR' );
	
					$query = "SELECT * FROM projects WHERE project_id='".addslashes($aFormValues['project_id'])."' LIMIT 1";
					$res = $this->ccdSQL->query($query);
					if ($row = $this->ccdSQL->fetch_assoc($res)) 
					{
						$aFormValues['dir'] = str_replace("/".$row['project_dir'],'',$aFormValues['dir']);
						$aFormValues['dir'] = "/".$row['project_dir'].$aFormValues['dir'];
					}
					break;

				case "extensions":
					$root_dir = $this->config->get( 'EXTENSION_DIR' );
					break;

					default : $root_dir = $this->config->get('MEDIA_DIR');
					break;
			}
			$file_name =  $_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];
			$file_tmp_name =  $_FILES['obj']['tmp_name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];
			
			
			$dest = $root_dir.$aFormValues['path']."/".$file_name;


			$res = @move_uploaded_file( $file_tmp_name, $dest );
			if (is_file($dest)) chmod($dest,$this->config->get('FILE_CHMOD'));

			if ( $res )
			{

				$objResponse->script( "this.preview.innerHTML='".$img."';" );
				$objResponse->script( "this.label.innerHTML='".$file_name."';" );
				$objResponse->script( "this.file.value='".$file_name."';" );
				$objResponse->script( "this.dir.value='".$aFormValues['path']."';" );

				return $objResponse;
			}
		}

		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------


	function ContentEditorImageUpload( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

	if ( is_array( $_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']] ) )
		{
			
			switch ( $aFormValues['root'] )
			{
				case "media":
					$root_dir = $this->config->get( 'MEDIA_DIR' );
	
					$query = "SELECT * FROM projects WHERE project_id='".addslashes($aFormValues['project_id'])."' LIMIT 1";
					$res = $this->ccdSQL->query($query);
					if ($row = $this->ccdSQL->fetch_assoc($res)) 
					{
						$aFormValues['dir'] = str_replace("/".$row['project_dir'],'',$aFormValues['dir']);
						$aFormValues['dir'] = "/".$row['project_dir'].$aFormValues['dir'];
					}
					break;

				case "extensions":
					$root_dir = $this->config->get( 'EXTENSION_DIR' );
					break;

					default : $root_dir = $this->config->get('MEDIA_DIR');
					break;
			}
			$file_name =  $_FILES['obj']['name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];
			$file_tmp_name =  $_FILES['obj']['tmp_name'][$aFormValues['obj_id']][$aFormValues['fieldname']]['up'];
			
			
			$dest = $root_dir.$aFormValues['path'].$file_name;
			$res = @move_uploaded_file( $file_tmp_name, $dest );
			if (is_file($dest)) chmod($dest,$this->config->get('FILE_CHMOD'));

			if ( $res )
			{

				$iCache = ccdsImageCache::getInstance();

				$img = $iCache->img_cache_show( array
				(
					"src" => "/media/".$aFormValues['path'].$file_name,
					"id" => "img".$aFormValues['fieldname']."_".$aFormValues['obj_id'],
					"width" => 120,
					"height" => 120
				));
				
				$objResponse->script( "this.preview.innerHTML='".$img."';" );
				$objResponse->script( "this.label.innerHTML='".$file_name."';" );
				$objResponse->script( "this.file.value='".$file_name."';" );
				$objResponse->script( "this.dir.value='".$aFormValues['path']."';" );

				return $objResponse;
			}
		}

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorParameter( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['project_id'] )
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$_SESSION['CMS']['ContentEditor'] = "parameter";

		$obj = $this->tools->getObjectDetails( $aFormValues['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
		$obj['permissions'] = $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'], $_SESSION['CMS']['user_level'] );
		$this->smarty->assign( "obj", $obj );

		$classes = $this->tools->getObjectClasses( $aFormValues['obj_id'] );
		$this->smarty->assign( "classes", $classes );
		$query = "SELECT * FROM obj_index WHERE obj_parent_obj_id='".$aFormValues['obj_id']."'";
		$res = $this->ccdSQL->query( $query );
		$elements = array ();

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$elements[] = $row;
		}
		$this->smarty->assign( "elements", $elements );

		if ( $aFormValues['obj_vers_id'] == "" )
		{

			$query = "SELECT * FROM obj_versions WHERE obj_vers_key_obj_id='".$aFormValues['obj_id']."' AND obj_vers_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."' ORDER BY obj_vers_created desc LIMIT 1";
			$res = $this->ccdSQL->query( $query );

			if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			{
				$obj_vers_id = $row['obj_vers_id'];
			}
			else
			{
				$obj_vers_id = false;
			}
		}

		$object = new ccdsObject( $aFormValues['obj_id'] );
		$aFormFields = $object->getParameterArray( $_SESSION['CMS']['lang'][$aFormValues['project_id']] );

		$this->smarty->assign( "aFormFields", $aFormFields );

		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_parameter.tpl" );
		$objResponse->assign( "ContentParameter_".$aFormValues['obj_id'], "innerHTML", $sHtml );

		if ( is_array( $aFormFields ) )
		{
			foreach ( $aFormFields as $FormField )
			{
				if ( $FormField['formtype'] == "html" )
				{
					$objResponse->script( "
            	tinyMCE.execCommand('mceAddControl', false, xajax.$('".$FormField['fieldname']."_".$obj['obj_id']."'));
            " );
					$destroy_callback .= "
            	tinyMCE.execCommand('mceRemoveControl', false, xajax.$('".$FormField['fieldname']."_".$obj['obj_id']."'));
            ";
				}
			}
			$objResponse->script( "
				TabManagerContent_".$obj['obj_id'].".on('destroy','Cedit".$obj['obj_id']."',function () {
					".$destroy_callback."
				});	
		" );
		}
		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorSave( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( is_array( $aFormValues['obj'] ) )
		{
			foreach ( $aFormValues['obj'] as $obj_id => $obj_values )
			{
				$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $obj_id )."' LIMIT 1";
				$res = $this->ccdSQL->query( $query );
				$project = $this->ccdSQL->fetch_assoc( $res );
				$aFormValues['project_id'] = $project['obj_key_project_id'];

				$objResponse->clsSWFupload->destroyForm( "form_content_id_".$obj_id );
				$obj = $this->tools->getObjectDetails( $obj_id, $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
				$obj['permissions'] = $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'], $_SESSION['CMS']['user_level'] );

				$this->ccdSQL->query( "UPDATE obj_index SET obj_last_edit='".time()."', obj_last_edit_key_user_id='".$_SESSION['CMS']['user_id']."' WHERE obj_id='".$obj_id."'" );

				$object = new ccdsObject( $obj_id );
				$obj_vers_id = $object->save( $obj_values, $_SESSION['CMS']['lang'][$aFormValues['project_id']] );
				
				
				$query = "UPDATE obj_versions SET obj_vers_visible=0 WHERE obj_vers_key_obj_id=".$obj['obj_id']." AND obj_vers_key_lang_id=".$_SESSION['CMS']['lang'][$aFormValues['project_id']];
				$res = $this->ccdSQL->query($query);

				$query = "UPDATE obj_versions SET obj_vers_visible=1 WHERE obj_vers_id=".$obj_vers_id;
				$res = $this->ccdSQL->query($query);
				

				$obj = $object->getObject( $_SESSION['CMS']['lang'][$aFormValues['project_id']], true );
				$obj['permissions'] = $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $obj['obj_id'], $_SESSION['CMS']['user_level'] );
				$this->smarty->assign( "obj", $obj );

				$obj_versions = array ();
				$query = "SELECT * FROM obj_versions LEFT OUTER JOIN user ON obj_vers_creator = user_id
		           WHERE obj_vers_key_obj_id='".$obj_id."' AND obj_vers_key_lang_id = '".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."' ORDER BY obj_vers_created desc LIMIT 10";
				$res = $this->ccdSQL->query( $query );

				while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
				{
					$obj_versions[] = $row;
				}
				
				$this->smarty->assign( "obj_versions", $obj_versions );
				$this->smarty->assign( "obj_vers_id", $obj_vers_id );

				$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_edit_versions.tpl" );
				$objResponse->assign( "obj_".$obj_id."_versions_div", "innerHTML", $sHtml );
			}
		}
		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorVersionSave( $aFormValues )
	{
		if ( "" == $aFormValues['project_id'] )
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$objResponse = new ccdsResponse();
		$this->ccdSQL->query( "UPDATE obj_versions SET obj_vers_visible='0' WHERE obj_vers_key_obj_id='".$aFormValues['obj_id']."' AND obj_vers_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."'" );
		$this->ccdSQL->query( "UPDATE obj_versions SET obj_vers_visible='1' WHERE obj_vers_id='".$aFormValues['obj_vers_id']."'" );
		$objResponse->script( 'new Effect.Highlight("EditBoxContent",{startcolor:"#D6EAFB",endcolor:"#ffffff",restorecolor:"#ffffff"});' );
		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorParameterSave( $aFormValues )
	{
		$objResponse = new ccdsResponse();
		if ( is_array( $aFormValues['obj'] ) )
		{
			foreach ( $aFormValues['obj'] as $obj_id => $obj_values )
			{
				if ( "" == $obj_values['project_id'] )
				{

					$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $obj_id )."' LIMIT 1";
					$res = $this->ccdSQL->query( $query );
					$project = $this->ccdSQL->fetch_assoc( $res );
					$obj_values['project_id'] = $project['obj_key_project_id'];
				}

				$this->ccdSQL->query( "UPDATE obj_index SET obj_last_edit='".time()."', obj_last_edit_key_user_id='".$_SESSION['CMS']['user_id']."' WHERE obj_id='".$obj_id."'" );
				$object = new ccdsObject( $obj_id );
				$foo = $object->saveParameter( $obj_values, $_SESSION['CMS']['lang'][$obj_values['project_id']] );
			}
		}
		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorMarkObjectDeleted( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		$obj_id = (int) $aFormValues['obj_id'];
		$query = "SELECT * FROM obj_index WHERE obj_id='".$obj_id."'";
		$res = $this->ccdSQL->query( $query );
		$obj = $this->ccdSQL->fetch_assoc( $res );
		$this->tools->MarkObjectDeleted( $obj_id, true );

		$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
			(
				"project_id" => $obj['obj_key_project_id'],
				"obj_parent_obj_id" => $obj['obj_parent_obj_id']
			),true);


		$objResponse->loadClassCommand( "ccdsTrash", "TrashReload", ( array ("project_id" => $obj['obj_key_project_id'])) );

		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------
	function ContentEditorPermissions( $aFormValues )
	{
		if ( "" == $aFormValues['project_id'] )
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$objResponse = new ccdsResponse();

		$obj = $this->tools->getObjectDetails( $aFormValues['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
		;

		$this->smarty->assign( "obj", $obj );

		$classes = $this->tools->getObjectClasses( $aFormValues['obj_id'] );
		$this->smarty->assign( "classes", $classes );
		$groups = Array ();
		$query = "SELECT * FROM groups order by group_name";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$tmprow = $row;
			$tmprow['permissions'] = $this->tools->getObjGroupPermissions( $aFormValues['obj_id'], $tmprow['group_id'] );
			$tmprow['user'] = array ();
			$query = "SELECT user.* FROM user LEFT JOIN mn_user_groups ON user_groups_key_user_id=user_id WHERE user_groups_key_group_id='".$row['group_id']."'";
			$res_user = $this->ccdSQL->query( $query );

			while ( $row_user = $this->ccdSQL->fetch_assoc( $res_user ) )
			{
				$row_user['permissions'] = $this->tools->getObjUserPermissions( $aFormValues['obj_id'], $tmprow['group_id'], $row_user['user_id'] );
				$tmprow['user'][] = $row_user;
			}
			$groups[] = $tmprow;
		}
		$this->smarty->assign( "groups", $groups );
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_permissions.tpl" );
		$objResponse->assign( "ContentPermissions_".$obj['obj_id'], "innerHTML", $sHtml );
		return $objResponse;
	}


	// -------------------------------------------------------------------------------------------------------------------
	function ContentEditorPermissionsSave( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_group WHERE obj_index_2_group_key_obj_id='".$values['obj_id']."'" );
		$this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_user WHERE obj_index_2_user_key_obj_id='".$values['obj_id']."'" );

		$regex = "group_".$group_id."_user_([0-9]?)_right";
		$sHtml .= date( "d.m.Y H:i" )."<br />";

		$query = "SELECT * FROM groups order by group_name";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$query = "SELECT user.* FROM user LEFT JOIN mn_user_groups ON user_groups_key_user_id=user_id WHERE user_groups_key_group_id='".$row['group_id']."'";
			$res_user = $this->ccdSQL->query( $query );

			$this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_group
                  (
                  obj_index_2_group_key_obj_id,
                  obj_index_2_group_key_group_id,
                  obj_index_2_group_read,
                  obj_index_2_group_modify,
                  obj_index_2_group_delete,
                  obj_index_2_group_new
                  )
                  VALUES (
                  '".$values['obj_id']."',
                  '".$row['group_id']."',
                  '".$values['group_'.$row['group_id'].'_read']."',
                  '".$values['group_'.$row['group_id'].'_modfiy']."',
                  '".$values['group_'.$row['group_id'].'_delete']."',
                  '".$values['group_'.$row['group_id'].'_new']."'
                  )
                  " );
			$sHtml .= $this->ccdSQL->error()."<br />";

			while ( $row_user = $this->ccdSQL->fetch_assoc( $res_user ) )
			{
				$this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_user
                      (
                      obj_index_2_user_key_obj_id,
                      obj_index_2_user_key_group_id,
                      obj_index_2_user_key_user_id,
                      obj_index_2_user_read,
                      obj_index_2_user_modify,
                      obj_index_2_user_delete,
                      obj_index_2_user_new
                      )
                      VALUES (
                      '".$values['obj_id']."',
                      '".$row['group_id']."',
                      '".$row_user['user_id']."',
                      '".$values['group_'.$row['group_id'].'_user_'.$row_user['user_id'].'_read']."',
                      '".$values['group_'.$row['group_id'].'_user_'.$row_user['user_id'].'_modify']."',
                      '".$values['group_'.$row['group_id'].'_user_'.$row_user['user_id'].'_delete']."',
                      '".$values['group_'.$row['group_id'].'_user_'.$row_user['user_id'].'_new']."'
                      )
                      " );
			}
		}

		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------
	function ContentEditorPermissionsClassesOpen( $aFormValues )
	{
		if ( "" == $aFormValues['project_id'] )
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$objResponse = new ccdsResponse();

		$obj = $this->tools->getObjectDetails( $aFormValues['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']], false );
		$this->smarty->assign( "obj", $obj );
		$classes = array ();
		$query = "SELECT
							*
							FROM
							classes
							LEFT JOIN
							permissions_class_2_class ON class_2_class_child_id = class_id
							
							LEFT OUTER JOIN permissions_obj_index_2_class ON
							(
								obj_index_class_key_class_id = class_2_class_child_id
								AND
								obj_index_class_key_obj_id = '".$obj['obj_id']."'
							)
							
							WHERE
							class_2_class_node_id='".$obj['obj_key_class_id']."'
							ORDER BY
							class_name
           ";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$classes[] = $row;
		}

		$this->smarty->assign( "classes", $classes );
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."content_classes.tpl" );
		$objResponse->assign( "ContentPermissionClasses_".$obj['obj_id'], "innerHTML", $sHtml );
		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------


	// -------------------------------------------------------------------------------------------------------------------
	function ContentEditorPermissionsClassesSave( $aFormValues )
	{

		$objResponse = new ccdsResponse();


		$this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_class WHERE obj_index_class_key_obj_id='".addslashes($aFormValues['obj_id'])."'" );

		if ( $aFormValues['apply_linked'] == 1 )
		{
			$query = "SELECT obj_id FROM obj_index WHERE obj_link_obj_id='".addslashes($aFormValues['obj_id'])."'";
			$res = $this->ccdSQL->query( $query );

			while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			{
				$this->ccdSQL->query( "DELETE FROM permissions_obj_index_2_class WHERE obj_index_class_key_obj_id='".addslashes($row['obj_id'])."'" );
			}
		}

		if ( is_array( $aFormValues['class'] ) )
			foreach ( $aFormValues['class'] as $class )
			{
				$this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_class VALUES('".addslashes($aFormValues['obj_id'])."','".addslashes($class)."')" );

				if ( $aFormValues['apply_linked'] == 1 )
				{
					$query = "SELECT obj_id FROM obj_index WHERE obj_link_obj_id='".addslashes($aFormValues['obj_id'])."'";
					$res = $this->ccdSQL->query( $query );

					while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
					{
						$this->ccdSQL->query( "INSERT INTO permissions_obj_index_2_class VALUES('".$row['obj_id']."','".addslashes($class)."')" );
					}
				}
			}
		$objResponse->loadcommands($this->ContentEditorOverview( $aFormValues ) );
		return $objResponse;
	}


	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorOverviewUpdate( $aFormValues )
	{
		if ( "" == $aFormValues['project_id'] )
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$objResponse = new ccdsResponse();
		
		$project_languages = array ();
		$query = "
           SELECT * FROM mn_project_language LEFT JOIN languages ON project_language_key_language_id=lang_id WHERE project_language_key_project_id='".$aFormValues['project_id']."'";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			if ( $aFormValues['obj_is_public'][$row['lang_id']] == "on" )
			{
				$aFormValues['obj_is_public'][$row['lang_id']] = 1;
			}
			else
			{
				$aFormValues['obj_is_public'][$row['lang_id']] = 0;
			}

			if ( $aFormValues['visible_childs'][$row['lang_id']] == "on" )
			{
				$aFormValues['visible_childs'][$row['lang_id']] = true;
			}
			else
			{
				$aFormValues['visible_childs'][$row['lang_id']] = false;
			}
			$this->update_visibility( $aFormValues['obj_id'], $row['lang_id'], $aFormValues['obj_is_public'][$row['lang_id']], $aFormValues['visible_childs'][$row['lang_id']] );

			
			if ($row['lang_id'] == $_SESSION['CMS']['lang'][$aFormValues['project_id']]) {
				if (1 == $aFormValues['obj_is_public'][$row['lang_id']]) {
					$objResponse->script('
						$("#CMS_project_tree_'.$aFormValues['project_id'].'").xTree("enable","navi_obj_'.$aFormValues['obj_id'].'", '.json_encode($aFormValues['visible_childs'][$row['lang_id']]).');		
					');
				} else {
					$objResponse->script('
						$("#CMS_project_tree_'.$aFormValues['project_id'].'").xTree("disable","navi_obj_'.$aFormValues['obj_id'].'", '.json_encode($aFormValues['visible_childs'][$row['lang_id']]).');		
					');
				}
			}
			if ( $aFormValues['apply_linked'] == 1 )
			{
				$query = "SELECT obj_id FROM obj_index WHERE obj_link_obj_id='".$aFormValues['obj_id']."'";
				$res2 = $this->ccdSQL->query( $query );

				while ( $row2 = $this->ccdSQL->fetch_assoc( $res2 ) )
				{
					$this->update_visibility( $row2['obj_id'], $row['lang_id'], $aFormValues['obj_is_public'][$row['lang_id']], $aFormValues['visible_childs'][$row['lang_id']] );
				}
			}
		}

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorExpire( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		if ( $aFormValues['obj_expire'] != 1 )
		{
			$aFormValues['obj_expire'] = 0;
			$aFormValues['obj_expire_start'] = 0;
			$aFormValues['obj_expire_end'] = 0;
		}else
		{
			$aFormValues['obj_expire_start'] = mktime( $aFormValues['obj_expire_start_hour'], $aFormValues['obj_expire_start_minute'], 0, $aFormValues['obj_expire_start_month'], $aFormValues['obj_expire_start_day'], $aFormValues['obj_expire_start_year'] );
			$aFormValues['obj_expire_end'] = mktime( $aFormValues['obj_expire_end_hour'], $aFormValues['obj_expire_end_minute'], 0, $aFormValues['obj_expire_end_month'], $aFormValues['obj_expire_end_day'], $aFormValues['obj_expire_end_year'] );
		}

		if ( $aFormValues['obj_expire_del'] != 1 )
			$aFormValues['obj_expire_del'] = 0;

		$query = "
           UPDATE obj_index SET
           obj_expire = '".$aFormValues['obj_expire']."',
           obj_expire_start = '".$aFormValues['obj_expire_start']."',
           obj_expire_end = '".$aFormValues['obj_expire_end']."',
           obj_expire_del = '".$aFormValues['obj_expire_del']."'
           WHERE
           obj_id = '".$aFormValues['obj_id']."'

           ";
		$this->ccdSQL->query( $query );

		if ( $aFormValues['apply_linked'] == 1 )
		{
			$query = "SELECT obj_id FROM obj_index WHERE obj_link_obj_id='".$aFormValues['obj_id']."'";
			$res = $this->ccdSQL->query( $query );

			while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
			{
				$query = "
                   UPDATE obj_index SET
                   obj_expire = '".$aFormValues['obj_expire']."',
                   obj_expire_start = '".$aFormValues['obj_expire_start']."',
                   obj_expire_end = '".$aFormValues['obj_expire_end']."',
                   obj_expire_del = '".$aFormValues['obj_expire_del']."'
                   WHERE
                   obj_id = '".$row['obj_id']."'
                   ";
				$this->ccdSQL->query( $query );
			}
		}
		$objResponse->script( 'new Effect.Highlight("DivContentEditorExpire",{startcolor:"#ffffff",endcolor:"#D6EAFB",restorecolor:"#ffffff"});' );

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorOverviewAction( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$query = "SELECT * FROM obj_index WHERE obj_id='".$aFormValues['obj_id']."'";
		$res = $this->ccdSQL->query( $query );
		$obj = $this->ccdSQL->fetch_assoc( $res );

		$obj_names = array ();
		$query = "
          SELECT
          *
          FROM
          obj_names

          WHERE
          obj_name_key_obj_id='".$aFormValues['obj_id']."'
          ";
		$res = $this->ccdSQL->query( $query );

		while ( $obj_name = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$obj_names[$obj_name['obj_name_key_lang_id']] = $obj_name['obj_name_name'];
		}
		
		$aFormValues['obj_action_recursive'] = isset($aFormValues['obj_action_recursive']) ? true : false;
		switch ( $aFormValues['obj_action'] )
		{
			case "copy":
				$_SESSION['CMS']['ACTION']['obj_id'] = $aFormValues['obj_id'];
				$_SESSION['CMS']['ACTION']['recursive'] = $aFormValues['obj_action_recursive'];
				$_SESSION['CMS']['ACTION']['do'] = "copy";
				$_SESSION['CMS']['ACTION']['name'] = $obj_names;
				$_SESSION['CMS']['ACTION']['obj'] = $obj;
				break;

			case "cut":
				$_SESSION['CMS']['ACTION']['obj_id'] = $aFormValues['obj_id'];
				$_SESSION['CMS']['ACTION']['recursive'] = $aFormValues['obj_action_recursive'];
				$_SESSION['CMS']['ACTION']['do'] = "cut";
				$_SESSION['CMS']['ACTION']['name'] = $obj_names;
				$_SESSION['CMS']['ACTION']['obj'] = $obj;
				break;

			case "del":

				$query = "SELECT COUNT(obj_id) as anzahl FROM obj_index WHERE obj_link_obj_id='".$aFormValues['obj_id']."'";
				$res = $this->ccdSQL->query( $query );
				$row = $this->ccdSQL->fetch_assoc( $res );

				if ( $row['anzahl'] > 0 AND $aFormValues['confirmed'] != 1 )
				{
					$objResponse->script( "ConfirmWin('Das gewählte Objekt ist ".$row['anzahl']
						." mal verlinkt.<br/><br/>Wollen Sie dieses Objekt wirklich löschen? Es gehen alle Links verloren!',function(){xajax_ContentEditorOverviewAction(xajax.objectToXML({'obj_id':".$aFormValues['obj_id']
						.",'obj_action':'del','confirmed':1}));return true;});" );
				}
				else
				{
					$this->tools->MarkObjectDeleted( $aFormValues['obj_id'], true );

					$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
						(
							"project_id" => $obj['obj_key_project_id'],
							"obj_parent_obj_id" => $obj['obj_parent_obj_id']
						),true);

					$objResponse->clsTabManager->close( "topTab", "tab_".$aFormValues['obj_id'] );

					if ( $row['anzahl'] > 0 )
					{
						$query = "SELECT obj_id,obj_parent_obj_id FROM obj_index WHERE obj_link_obj_id='".$aFormValues['obj_id']."'";
						$res = $this->ccdSQL->query( $query );

						while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
						{
							$this->ccdSQL->query( "DELETE FROM obj_index WHERE obj_id='".$row['obj_id']."'" );
							$objResponse->clsTabManager->close( "topTab", "tab_".$row['obj_id'] );
						}
					}

					if ($aFormValues['frontend']) 		$objResponse->script("xajax.$('CMS_FRONTEND_IFRAME').contentWindow.location.reload();");

					$objResponse->loadClassCommand( "ccdsTrash", "TrashReload", ( array ("project_id" => $obj['obj_key_project_id'])) );
				}
				break;
				
			case "clone":

				$new_obj_id = $this->tools->copy_object($aFormValues['obj_id'],$obj['obj_parent_obj_id'],$_SESSION['CMS']['user_id'],$aFormValues['obj_action_recursive'],$_SESSION['CMS']['ACTION']['recursive']);
				$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
					(
						"project_id" => $obj['obj_key_project_id'],
						"obj_parent_obj_id" => $obj['obj_parent_obj_id']
					),true);	
								
			break;
			case "paste":

				if ( $_SESSION['CMS']['ACTION']['do'] == "cut" )
				{

					$query = "SELECT MAX(obj_position) as pos FROM obj_index WHERE obj_parent_obj_id='".$aFormValues['obj_id']."'";
					$res = $this->ccdSQL->query( $query );
					$pos = $this->ccdSQL->fetch_assoc( $res );
					
					
					$this->ccdSQL->query( "UPDATE obj_index SET obj_parent_obj_id='".addslashes($aFormValues['obj_id'])."',obj_position='".( $pos['pos'] + 1 )."' WHERE obj_id='".$_SESSION['CMS']['ACTION']['obj_id']."'" );

					$query = "SELECT obj_parent_obj_id,obj_key_project_id FROM obj_index WHERE obj_id='".addslashes($_SESSION['CMS']['ACTION']['obj_id'])."'";
					$res = $this->ccdSQL->query($query);
					$parent = $this->ccdSQL->fetch_assoc($res);

					$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
						(
							"project_id" => $parent['obj_key_project_id'],
							"obj_parent_obj_id" => $parent['obj_parent_obj_id']
						),true );	
				}

				if ( $_SESSION['CMS']['ACTION']['do'] == "copy" )
				{
					$new_obj_id = $this->tools->copy_object($_SESSION['CMS']['ACTION']['obj']['obj_id'],$aFormValues['obj_id'],$_SESSION['CMS']['user_id'],$_SESSION['CMS']['ACTION']['recursive']);
				}

				$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
					(
						"project_id" => $obj['obj_key_project_id'],
						"obj_parent_obj_id" => $obj['obj_id']
					),true);	

				$_SESSION['CMS']['ACTION'] = "";
				break;

				default:
				$_SESSION['CMS']['ACTION']="";
				break;
		}

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorDynamicTemplateNew( $aFormValues )
	{
		if ( "" == $aFormValues['project_id'] )
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}


		$objResponse = new ccdsResponse();

		$object = new ccdsObject($aFormValues['obj_id']);
		$obj = $object->getObject($_SESSION['CMS']['lang'][$aFormValues['project_id']]);
		
		$ObjectConfig = ccdsObjectConfig::getInstance();
		$classConfig = $ObjectConfig->getClass( $obj['obj_key_class_id'] );

		$fieldConfig = &$classConfig['data']['columns'][$aFormValues['fieldname']];
	
		if (!is_array($fieldConfig['templates'])) $fieldConfig['templates'] = array();
		
		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function ContentEditorDynamicFieldAdd( $aFormValues )
	{
		if ( "" == $aFormValues['project_id'] )
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes( $aFormValues['obj_id'] )."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];
		}

		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['field']['fieldlabel'] ) {
		//$objResponse->alert("Achtung, es wurde keine Bezeichnung angegeben");
		//return $objResponse;
		}

		$obj = $this->tools->getObjectDetails( $aFormValues['obj_id'], $_SESSION['CMS']['lang'][$aFormValues['project_id']] );
		$this->smarty->assign( "obj", $obj );

		$content = array
		(
			"label" => $aFormValues['field']['fieldlabel'],
			"fieldname" => $aFormValues['field']['fieldlabel'],
			"suffix" => "[value]",
			"obj_id" => $aFormValues['obj_id'],
			"obj_vers_id" => $aFormValues['obj_vers_id']
		);

		$this->smarty->assign( "content", $content );
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."forms/".$aFormValues['field']['fieldtype'].".tpl" );

		$sHtml .= "<input type=\"hidden\" name=\"".$aFormValues['fieldname']."[".$aFormValues['field']['fieldlabel']."][formtype]"."\" value=\"".$aFormValues['field']['fieldtype']."\"/>";
		$sHtml .= "<input type=\"hidden\" name=\"".$aFormValues['fieldname']."[".$aFormValues['field']['fieldlabel']."][label]"."\" value=\"".$aFormValues['field']['fieldlabel']."\"/>";

		$objResponse->append( "dyn_area_".$aFormValues['fieldname']."_".$aFormValues['obj_id'], "innerHTML", $sHtml );

		return $objResponse;
	}


	// -------------------------------------------------------------------------------------------------------------------

	function update_visibility( $obj_id, $lang_id, $value, $recursive = false )
	{
		$this->ccdSQL->query( "DELETE FROM obj_index_public WHERE objp_key_obj_id='".$obj_id."' AND objp_key_lang_id='".$lang_id."'" );

		if ( $value == 1 )
			$this->ccdSQL->query( "INSERT INTO obj_index_public VALUES('".$obj_id."','".$lang_id."')" );

		if ( $recursive != false )
		{
			$query = "SELECT * FROM obj_index WHERE obj_parent_obj_id='".$obj_id."'";
			$res = $this->ccdSQL->query( $query );

			while ( $row = $this->ccdSQL->fetch_array( $res ) )
			{
				$this->update_visibility( $row['obj_id'], $lang_id, $value, true );
			}
		}
	}

// -------------------------------------------------------------------------------------------------------------------

}
